package org.jeecg.modules.fk.service;

import org.jeecg.modules.fk.mapper.auto.model.RiskOldNewWithBLOBs;
import org.jeecg.modules.fk.vo.indicator.IndicatorDefineVO;
import org.jeecg.modules.fk.constant.ZbTypeEnum;
import org.jeecg.modules.fk.vo.indicator.ZbDefineVO;
import org.jeecg.modules.fk.vo.old_new.RiskOldNewGroupItemVO;
import org.jeecg.modules.fk.vo.old_new.RiskOldNewGroupVO;
import org.jeecg.modules.fk.vo.old_new.RiskOldNewVO;
import org.jeecg.modules.fk.repository.FkDaoRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 新旧版本指标对照(与税检宝) 相关
 */
@Slf4j
@Service
public class RiskOldNewService {
    @Autowired
    private FkDaoRepository daoRepository;
    @Autowired
    private RiskIndicatorService riskIndicatorService;

    /**
     * 通过旧的 指标编码（税检宝） 获取新的数据
     *
     * @param zbbhList
     */
    public Map<String, RiskOldNewVO> queryZbByOld(List<String> zbbhList) {
        List<RiskOldNewWithBLOBs> list = this.daoRepository.getRiskOldNewExtMapper().queryZbByOld(zbbhList);
        if (list == null || list.isEmpty()) {
            return new HashMap<>();
        }

        return list.stream()
                .map(e -> {
                    RiskOldNewVO riskOldNewVO = new RiskOldNewVO();
                    BeanUtils.copyProperties(e, riskOldNewVO);
                    riskOldNewVO.setYwxlDm(StringUtils.substringBefore(e.getNewZbbh(), "-"));
                    riskOldNewVO.setYwdlDm(e.getYwdlDm());
                    return riskOldNewVO;
                })
                .collect(Collectors.toMap(RiskOldNewVO::getOldZbbm, e -> e));

    }

    /**
     * 通过类型分组数据
     *
     * @param zbType
     */
    public List<RiskOldNewGroupVO> groupZbByType(ZbTypeEnum zbType) {
        Map<String, IndicatorDefineVO> defineMap = this.riskIndicatorService.getDefineMap(zbType);


        List<RiskOldNewWithBLOBs> list = this.daoRepository.getRiskOldNewExtMapper().queryZbByYwdlDm(zbType.toString());
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }

        //分组
        Map<String, List<RiskOldNewGroupItemVO>> itemMap = list.stream()
                .map(e -> {
                    RiskOldNewGroupItemVO vo = new RiskOldNewGroupItemVO();
                    BeanUtils.copyProperties(e, vo);

                    IndicatorDefineVO indicatorDefineVO = defineMap.get(vo.getNewZbbh());
                    if(indicatorDefineVO != null){
                        ZbDefineVO zbDefineVO = new ZbDefineVO();
                        BeanUtils.copyProperties(indicatorDefineVO, zbDefineVO);
                        vo.setIndicatorDefine(zbDefineVO);
                    }
                    return vo;
                })
                .collect(Collectors.groupingBy(e -> StringUtils.substringBefore(e.getNewZbbh(), "-")));


        //分组，并组装数据
        return list.stream()
                .map(e -> {
                    RiskOldNewGroupVO riskOldNewGroupVO = new RiskOldNewGroupVO();
                    riskOldNewGroupVO.setYwxlDm(StringUtils.substringBefore(e.getNewZbbh(), "-"));
                    riskOldNewGroupVO.setYwxlMc(e.getNewYwxl());
                    riskOldNewGroupVO.setYwdlDm(e.getYwdlDm());
                    return riskOldNewGroupVO;
                })
                .distinct()
                .sorted(Comparator.comparing(RiskOldNewGroupVO::getYwxlDm))
                .map(e -> {
                    e.setItems(itemMap.get(e.getYwxlDm()));
                    e.setYwxlSl(e.getItems().size());
                    return e;
                })
                .collect(Collectors.toList());


    }

}
